from django import forms
from django.core.urlresolvers import reverse
from django.contrib.flatpages.models import FlatPage
from django.contrib import admin
from django.contrib.flatpages.admin import FlatpageForm, FlatPageAdmin
from django.utils.translation import ugettext_lazy as _
from models import ExtendedFlatPage
from django.conf import settings
from django.utils.log import logger

try:
    from tinymce.widgets import TinyMCE
except ImportError, e:
    logger.warning('tiny mce not installed')    

class ExtendedFlatPageForm(FlatpageForm):
    class Meta:
        model = ExtendedFlatPage

try:
    from mptt.admin import MPTTModelAdmin
    class ExtendedFlatPageAdmin(MPTTModelAdmin):
            def formfield_for_dbfield(self, db_field, **kwargs):
                if db_field.name == 'content' & 'tinymce' in settings.INSTALLED_APPS:
                    return forms.CharField(widget=TinyMCE(
                                                          attrs={'cols': 80, 'rows': 30},
                                                          mce_attrs={'external_link_list_url': reverse('tinymce.views.flatpages_link_list')},
                                                          ))
                return super(ExtendedFlatPageAdmin, self).formfield_for_dbfield(db_field, **kwargs)
            form = ExtendedFlatPageForm
            fieldsets = (
                         (None, {'fields': ('url', 'title', 'content', 'sites', 'order', 'parent','inmenu')}),
                (_('Advanced options'), {'classes': ('collapse',), 'fields': ('enable_comments', 'registration_required', 'template_name')}),
            )
            list_filter = ('sites', 'enable_comments', 'registration_required')
            search_fields = ('url', 'title')
    admin.site.unregister(FlatPage)
    admin.site.register(ExtendedFlatPage, ExtendedFlatPageAdmin)
except ImportError, e:
    logger.debug('MPTTModelAdmin not found')
    class ExtendedFlatPageAdmin(FlatPageAdmin):
        def formfield_for_dbfield(self, db_field, **kwargs):
            if db_field.name == 'content' & 'tinymce' in settings.INSTALLED_APPS:
                return forms.CharField(widget=TinyMCE(
                                                      attrs={'cols': 80, 'rows': 30},
                                                      mce_attrs={'external_link_list_url': reverse('tinymce.views.flatpages_link_list')},
                                                      ))
            return super(ExtendedFlatPageAdmin, self).formfield_for_dbfield(db_field, **kwargs)
    
        form = ExtendedFlatPageForm
        fieldsets = (
                     (None, {'fields': ('url', 'title', 'content', 'sites', 'order', 'parent','inmenu')}),
            (_('Advanced options'), {'classes': ('collapse',), 'fields': ('enable_comments', 'registration_required', 'template_name')}),
        )
        list_filter = ('sites', 'enable_comments', 'registration_required')
        search_fields = ('url', 'title')
    admin.site.unregister(FlatPage)
    admin.site.register(ExtendedFlatPage, ExtendedFlatPageAdmin)
